
package com.studiofortress.sf.audio;

import de.jarnbjo.ogg.EndOfOggStreamException;
import de.jarnbjo.vorbis.VorbisStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * This is an input stream that wraps the given VorbisStream.
 *
 * <em>WARNING!</em> This is an incomplete implementation of the InputStream. Only the
 * methods implemented in this class should be used, all others should be
 * avoided!
 *
 * This should never be made a public class until it is fully implemented!
 *
 * @author Joseph Lenton - JosephLenton@StudioFortress.com
 */
class VorbisInputStream extends InputStream
{
    private final VorbisStream source;

    public VorbisInputStream(VorbisStream source)
    {
        this.source = source;
    }

    @Override
    public int read()
            throws IOException
    {
        return 0;
    }

    @Override
    public int read(byte[] buffer)
            throws IOException
    {
        return read(buffer, 0, buffer.length);
    }

    @Override
    public int read(byte[] buffer, int offset, int length)
            throws IOException
    {
        try {
            return source.readPcm(buffer, offset, length);
        } catch(EndOfOggStreamException e) {
            return -1;
        }
    }

    @Override
    public void close() throws IOException
    {
        source.close();
        super.close();
    }
}